
function swicthcountry(countryid,rownow)
{
  if(countryid==210)
  {
     $("#feature"+rownow+' .prothaiclass').show();
     $("#feature"+rownow+' .prootherclass').hide();
 // $("#feature"+rownow+' select[name="proid[]"]').show();
  //$("#feature"+rownow+' select[name="disid[]"]').show();
 // $("#feature"+rownow+' select[name="tumid[]"]').show();
  
  //$("#feature"+rownow+' input[name="state[]"]').hide();
  
  
  }else
  {
      $("#feature"+rownow+' .prothaiclass').hide();
     $("#feature"+rownow+' .prootherclass').show();
 // $("#feature"+rownow+' select[name="proid[]"]').hide();
 // $("#feature"+rownow+' select[name="disid[]"]').hide();
 // $("#feature"+rownow+' select[name="tumid[]"]').hide(); 
  
  
//  $("#feature"+rownow+' input[name="state[]"]').show();
  
  }

}


function addformcontact()
{

   
    $("#addform").append('<div id="feature'+rowdiv+'" class="itPlg addCtcPlg featureclass" >'+dataform['contact']+"</div>");
    var rownow=rowdiv;
    $("#feature"+rownow+' input[name="alltime[]"]').click(function() {
    
    if($(this).is(":checked"))
    {
     //$("#feature"+rownow+' input[name="openhours[]"]').attr("disabled", "disabled");
    // $("#feature"+rownow+' input[name="closehours[]"]').attr("disabled", "disabled");
         
                              
    }else
    {
   //   $("#feature"+rownow+' input[name="openhours[]"]').removeAttr("disabled");  
  //    $("#feature"+rownow+' input[name="closehours[]"]').removeAttr("disabled");  

    }
    
    });
    
    
    $("#feature"+rownow+' input[name="sameaddressprofile[]"]').click(function() {
    
    if($(this).is(":checked"))
    {
     $.post(serviceinfo+'getmyuser', { select:['address','countryid','disid','tumid','proid','code','state'] }, function(data) {
         
         //console.log(data);
         if(data.error.response)
         {
          alertbox(data.error.error);
         
         }else
         {
         
         if(data['data']['address'])$("#feature"+rownow+' textarea[name="address[]"]').val(data['data']['address']);
         
         if(data['data']['countryid'])$("#feature"+rownow+' select[name="countryid[]"]').val(data['data']['countryid']); 
       
         
         swicthcountry(data['data']['countryid'],rownow);
         
         if(data['data']['countryid']==210)
         {
         if(data['data']['proid']&&data['data']['proid']!="0")
         {

         $("#feature"+rownow+' select[name="proid[]"]').val(data['data']['proid']);
         
         var xmlRequest=getdistrict("#feature"+rownow+' select[name="disid[]"]',"#feature"+rownow+' select[name="tumid[]"]',data['data']['proid'],data['data']['disid']);
         
         $("#feature"+rownow+' select[name="disid[]"]').val(data['data']['disid']);
        // $(document).ajaxComplete(function() {
        
         xmlRequest.complete(function() { 
         
         //alert("second complete"); 
         if(data['data']['disid']&&data['data']['disid']!="0") {gettumbon("#feature"+rownow+' select[name="tumid[]"]',data['data']['disid'],data['data']['tumid']);}
         
         });
        
         
     
       //  });
         
        // if(data['data']['tumid']) $("#feature"+rownow+' select[name="tumid[]"]').val(data['data']['tumid']);
         
         }
         }
         else
         {
         
         if(data['data']['state']) $("#feature"+rownow+' input[name="state[]"]').val(data['data']['state']);
         
         }
         
         
         if(data['data']['code']) $("#feature"+rownow+' input[name="code[]"]').val(data['data']['code']);
         
         
         }
       //  alert("Data Loaded: " + data);
       
       
       });

         
                              
    }
    
    });
    
    
    $("#feature"+rownow+' input[name="samecontactprofile[]"]').click(function() {
    
    if($(this).is(":checked"))
    {
    
    $.post(serviceinfo+'getmyuser', { select:['facebook','twitter','googleplus','email','website','phone','fax','etc'] }, function(data) {
    
    
           if(data.error.response)
         {
          alertbox(data.error.error);
         
         }else
         {
          if(data['data']['facebook'])$("#feature"+rownow+' input[name="facebook[]"]').val(data['data']['facebook']); 
          if(data['data']['twitter'])$("#feature"+rownow+' input[name="twitter[]"]').val(data['data']['twitter']); 
          if(data['data']['googleplus'])$("#feature"+rownow+' input[name="googleplus[]"]').val(data['data']['googleplus']); 
          if(data['data']['email'])$("#feature"+rownow+' input[name="email[]"]').val(data['data']['email']);
          if(data['data']['website'])$("#feature"+rownow+' input[name="website[]"]').val(data['data']['website']);
          if(data['data']['phone'])$("#feature"+rownow+' input[name="phone[]"]').val(data['data']['phone']);
          if(data['data']['fax'])$("#feature"+rownow+' input[name="fax[]"]').val(data['data']['fax']);
          if(data['data']['etc'])$("#feature"+rownow+' textarea[name="etc[]"]').val(data['data']['etc']);
         
         }
    
    
    });
    
    }
    
    
    });
    
    
    
    $("#feature"+rownow+' select[name="countryid[]"]').change(function() 
    { 
     
       swicthcountry($(this).val(),rownow);
    
    });
    
    
    $("#feature"+rownow+' select[name="proid[]"]').change(function() 
    { 
    
       getdistrict("#feature"+rownow+' select[name="disid[]"]',"#feature"+rownow+' select[name="tumid[]"]',$(this).val());
    
    });
    
    $("#feature"+rownow+' select[name="disid[]"]').change(function() 
    {
    gettumbon("#feature"+rownow+' select[name="tumid[]"]',$(this).val());
    
    });
    
   $('#feature'+rownow+' .deleterowbutton').click(function(){
   
    $('#feature'+rownow).remove();
    
   });
    
    
    
    
    
    rowdiv++;

}

function editformcontact(datarow)
{
    addformcontact();
    var rownow= rowdiv-1;
    
    if(datarow.alltime!="0")
    {
    $("#feature"+rownow+' input[name="alltime[]"]').trigger('click');
    }else
    {
    

    $("#feature"+rownow+' input[name="openhours[]"]').val(datarow.openhours);
    $("#feature"+rownow+' input[name="closehours[]"]').val(datarow.closehours);

    
    
    }
    
    $("#feature"+rownow+' textarea[name="infomation[]"]').val(datarow.infomation);
    
     if(datarow.sameaddressprofile!="0")
    {
    $("#feature"+rownow+' input[name="sameaddressprofile[]"]').trigger('click');
    }else
    {
    $("#feature"+rownow+' textarea[name="address[]"]').val(datarow.address);
    if(datarow.countryid)
    {
    $("#feature"+rownow+' select[name="countryid[]"]').val(datarow.countryid);  
    swicthcountry(datarow.countryid,rownow);
    
    if(datarow.countryid=="210")
    {
     if(datarow.proid!="0")
     {
     $("#feature"+rownow+' select[name="proid[]"]').val(datarow.proid);
     var xmlRequest= getdistrict("#feature"+rownow+' select[name="disid[]"]',"#feature"+rownow+' select[name="tumid[]"]',datarow.proid);     
     xmlRequest.complete(function() { 
     if(datarow.disid)
     {
     $("#feature"+rownow+' select[name="disid[]"]').val(datarow.disid);
     gettumbon("#feature"+rownow+' select[name="tumid[]"]',datarow['disid'],datarow['tumid']);
     
     }
     
     });
     
     
     
     
     
     
     
     }
    
    }
    else
    {
    
    $("#feature"+rownow+' input[name="state[]"]').val(datarow.state);
    
    
    }
    
    $("#feature"+rownow+' input[name="code[]"]').val(datarow.code);
    
    
    
    
    }
    
    
    }
    
    
    
    
    if(datarow.samecontactprofile!="0")
    {
    $("#feature"+rownow+' input[name="samecontactprofile[]"]').trigger('click');
    }else
    {
    $("#feature"+rownow+' input[name="facebook[]"]').val(datarow.facebook);
    $("#feature"+rownow+' input[name="twitter[]"]').val(datarow.twitter);
    $("#feature"+rownow+' input[name="phone[]"]').val(datarow.phone);
    $("#feature"+rownow+' input[name="googleplus[]"]').val(datarow.googleplus);
    $("#feature"+rownow+' input[name="website[]"]').val(datarow.website);
    $("#feature"+rownow+' input[name="email[]"]').val(datarow.email);
    $("#feature"+rownow+' input[name="phone[]"]').val(datarow.phone);
    $("#feature"+rownow+' input[name="fax[]"]').val(datarow.fax);
    $("#feature"+rownow+' textarea[name="etc[]"]').val(datarow.etc);
    
    }
    
    

}

function saveformcontact(id,plugid)
{
var obj={};
var alltime,openhours,closehours,infomation,sameaddressprofile,address,countryid,proid,disid,tumid,state,code,samecontactprofile,facebook,twitter,googleplus,email,website,phone,fax,etc;

alltime=$("#"+id+' input[name="alltime[]"]').is(':checked');
if(alltime)
{
openhours="00:00";
closehours="00:00";
alltime=$("#"+id+' input[name="alltime[]"]').val();


}else
{
openhours=$("#"+id+' input[name="openhours[]"]').val();
closehours=$("#"+id+' input[name="closehours[]"]').val();
alltime=0;

}


infomation=$("#"+id+' textarea[name="infomation[]"]').val();
sameaddressprofile=$("#"+id+' input[name="sameaddressprofile[]"]').is(':checked');
if(sameaddressprofile)
{
sameaddressprofile=$("#"+id+' input[name="sameaddressprofile[]"]').val();
address="";
countryid=0;
proid=0;
disid=0;
tumid=0;
state="";
code="";
}else
{
sameaddressprofile=0;

address=$("#"+id+' textarea[name="address[]"]').val();

countryid=$("#"+id+' select[name="countryid[]"]').val();

proid=$("#"+id+' select[name="proid[]"]').val();

disid=$("#"+id+' select[name="disid[]"]').val();

tumid=$("#"+id+' select[name="tumid[]"]').val();

state=$("#"+id+' input[name="state[]"]').val();

code=$("#"+id+' input[name="code[]"]').val();

}

samecontactprofile=$("#"+id+' input[name="samecontactprofile[]"]').is(':checked');
if(samecontactprofile)
{
samecontactprofile=$("#"+id+' input[name="samecontactprofile[]"]').val();
facebook="";
twitter="";
googleplus="";
email="";
website="";
phone="";
fax="";
etc="";

}else
{
samecontactprofile=0;
facebook=$("#"+id+' input[name="facebook[]"]').val();
twitter=$("#"+id+' input[name="twitter[]"]').val();
googleplus=$("#"+id+' input[name="googleplus[]"]').val();
email=$("#"+id+' input[name="email[]"]').val();
website=$("#"+id+' input[name="website[]"]').val();
phone=$("#"+id+' input[name="phone[]"]').val();
fax=$("#"+id+' input[name="fax[]"]').val();
etc=$("#"+id+' textarea[name="etc[]"]').val();



}



obj={plugid:plugid,alltime:alltime,openhours:openhours,closehours:closehours,infomation:infomation,sameaddressprofile:sameaddressprofile,address:address,countryid:countryid,proid:proid,disid:disid,tumid:tumid,state:state,code:code,samecontactprofile:samecontactprofile,facebook:facebook,twitter:twitter,googleplus:googleplus,email:email,website:website,phone:phone,fax:fax,etc:etc};



return obj;






}

function GetTimeContact(date) {
var timeString = date;
var H = +timeString.substr(0, 2);
var h = H % 12 || 12;
var ampm = H < 12 ? "AM" : "PM";
timeString = h + timeString.substr(2, 3) + ampm;
return timeString;
    }
function ValidUrl(str) {
  var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
  '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
  '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
  '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
  '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
  '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  if(!pattern.test(str)) {
    return false;
  } else {
    return true;
  }
}
function validateEmail(email) 
{
    var re = /\S+@\S+\.\S+/;
    return re.test(email);
}

function getformcontact(dataplugin)
{
var code = '<div class="ldIt wrpCtcLd clBg4">\n';
code += '<div class="wrpOpenTimeAdrs">\n';
if((dataplugin.openhours!=""&&dataplugin.closehours!="")||dataplugin.alltime!="0")
{
code += '<div class="wrpOpenTimeLd">\n';

code += '<div class="wrpHdInfoGen mgBtm10">\n';
code += '<h2 class="clTxt2 fs04">Opening Times</h2>\n';
code += '</div>\n';
code += '<div class="OpenTimeLd">\n';
if(dataplugin.openhours!=""&&dataplugin.closehours!=""&&dataplugin.openhours!="00:00"&&dataplugin.closehours!="00:00")
{
code += '<span>'+GetTimeContact(dataplugin.openhours)+'</span>\n';
code += '<span class="clTxt2">to</span>\n';
code += '<span>'+GetTimeContact(dataplugin.closehours)+'</span><br>\n';
code += '<span>'+dataplugin.infomation+'</span>';
}else
{
code += '<span>open 24 hours</span>\n';

}


//code += '<p>Closed on Sunday.</p>\n';
code += '</div>\n';
code += '</div>\n';
}
code += '<div class="wrpAdrsInfo wrpAdrsLd">\n';
code += '<div class="wrpHdInfoGen mgBtm10">\n';
code += '<h2 class="clTxt2 fs04">Address</h2>\n';
code += '</div>\n';
var dataaddress="";
if(dataplugin.address)
{
     dataaddress=dataplugin.address;

}

var datatumname="";
if(dataplugin.tumname)
{
     datatumname=" "+dataplugin.tumname;

}


var datadisname="";
if(dataplugin.disname)
{
     datadisname=" "+dataplugin.disname;

}

var dataproname="";
if(dataplugin.proname)
{
     dataproname=" "+dataplugin.proname;

}

var datacode="";
if(dataplugin.code)
{
     datacode=" "+dataplugin.code;

}

var datacountryname="";
if(dataplugin.countryname)
{
     datacountryname=" "+dataplugin.countryname;

}

var datastate="";
if(dataplugin.state)
{
     datastate=" "+dataplugin.state;

}


var dataplugin


if(dataplugin.countryid=="210")
{

code += '<p>'+dataaddress+datatumname+datadisname+dataproname+datacode+datacountryname+'</p>\n';

}else
{
code += '<p>'+dataaddress+datastate+datacountryname+'</p>\n';

}

code += '</div>\n';
code += '</div>\n';
var displaynone="";
if( dataplugin.facebook.indexOf("facebook")==-1&&dataplugin.twitter.indexOf("twitter")==-1&&dataplugin.googleplus.indexOf("plus.google")==-1&&!dataplugin.website&&!dataplugin.email&&!dataplugin.phone&&!dataplugin.fax&&!dataplugin.etc   )
{
displaynone='style="display:none"';

}

code += '<div class="wrpCtcInfo" '+displaynone+'>\n';
code += '<div class="wrpHdInfoGen mgBtm10">\n';
code += '<h2 class="clTxt2 fs04">Contact</h2>\n';
code += '</div>\n';
code += '<table class="tbGenInfo tbCtcLd tbAddCtnt clTxt3 fs03">\n';
if(dataplugin.facebook.indexOf("facebook")!=-1)
{
var datafacebook=dataplugin.facebook;
if(dataplugin.facebook.indexOf("http")==-1)
{
    datafacebook="http://"+dataplugin.facebook;

}


code += '<tr>\n';
code += '<th>Facebook</th>\n';
code += '<td><a href="'+datafacebook+'" target="_blank">'+datafacebook+'</a></td>\n';
code += '</tr>\n';
}
if(dataplugin.twitter.indexOf("twitter")!=-1)
{
var datatwitter=dataplugin.twitter;
if(dataplugin.twitter.indexOf("http")==-1)
{
    datatwitter="http://"+dataplugin.twitter;

}
code += '<tr>\n';
code += '<th>Twitter</th>\n';
code += '<td><a href="'+datatwitter+'" target="_blank">'+datatwitter+'</a></td>\n';
code += '</tr>\n';
}
if(dataplugin.googleplus.indexOf("plus.google")!=-1)
{
var datatgoogleplus=dataplugin.googleplus;
if(dataplugin.googleplus.indexOf("http")==-1)
{
    datatgoogleplus="http://"+dataplugin.googleplus;

}

code += '<tr>\n';
code += '<th>Google+</th>\n';
code += '<td><a href="'+datatgoogleplus+'" target="_blank" >'+datatgoogleplus+'</a></td>\n';
code += '</tr>\n';

}
if(dataplugin.website)
{
if(ValidUrl(dataplugin.website))
{
code += '<tr>\n';
code += '<th>Website</th>\n';
code += '<td><a href="'+dataplugin.website+'" target="_blank" >'+dataplugin.website+'</a></td>\n';
code += '</tr>\n';
}
}
if(dataplugin.email)
{
if(validateEmail(dataplugin.email))
{
code += '<tr>\n';
code += '<th>Email</th>\n';
code += '<td>'+dataplugin.email+'</td>\n';
code += '</tr>\n';
}
}
if(dataplugin.phone)
{
code += '<tr>\n';
code += '<th>Phone</th>\n';
code += '<td>'+dataplugin.phone+'</td>\n';
code += '</tr>\n';
}
if(dataplugin.fax)
{
code += '<tr>\n';
code += '<th>Fax</th>\n';
code += '<td>'+dataplugin.fax+'</td>\n';
code += '</tr>\n';
}
if(dataplugin.etc)
{
code += '<tr>\n';
code += '<th>ETC</th>\n';
code += '<td>'+dataplugin.etc+'</td>\n';
code += '</tr>\n';
}
code += '</table>\n';

code += '</div>\n';


code += '<div class="emtGen"></div>\n';
code += '</div>';
return code;
}

